package com.fresh.service.impl;

import com.fresh.feign.OrderFeignClient;
import com.fresh.service.SecKillService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class SecKillServiceImpl implements SecKillService {


    @Autowired
    private RedisTemplate strRedisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private OrderFeignClient orderFeignClient;


    @Override
    public boolean rushGoods(Integer goodsId, Integer num, String address,HttpServletRequest httpServletRequest) {
        boolean flag = false;
        ListOperations list = strRedisTemplate.opsForList();
        if (list.leftPop("goodsKill::" + goodsId) != null){
            //TODO 还有库存，继续抢购
            //TODO 生成订单
            //TODO 商品库存-1
            //TODO 订单放入MQ
            orderFeignClient.add(goodsId,num,address,httpServletRequest);
            rabbitTemplate.convertAndSend("exchangeDlx","order.dlx","myOrder");
            flag = true;
        }
        return flag;
    }
}
